new Vue({
    el: '#app',
    data() {
        return {
            form: {
                Interface_BASE_URL: '',
                Business_BASE_URL: '',
                image: '',
                accordion: '1',
                collapse: '0',
                control: '0',
                controlWidth: 500,
                async: '1',
                enable: '1',
                keepState: '1',
                session: '0',
                preload: '0',
                max: 30,
                index: '2',
                defaultColor: '2',
                defaultMenu: 'dark-theme',
                defaultHeader: 'light-theme',
                allowCustom: '0',
                banner: '0',
                keepLoad: 1200,
                autoHead: '0',
                footer: '0',
            },
            rules: {
                Interface_BASE_URL: [{ required: true, message: '请输入接口域名', trigger: 'blur' }],
                Business_BASE_URL: [{ required: true, message: '请输入业务域名', trigger: 'blur' }]
            },
            page: [],
            colors: [{
                id: "1",
                color: "#2d8cf0",
                second: "#ecf5ff"
            }, {
                id: "2",
                color: "#36b368",
                second: "#f0f9eb"
            }, {
                id: "3",
                color: "#f6ad55",
                second: "#fdf6ec"
            }, {
                id: "4",
                color: "#f56c6c",
                second: "#fef0f0"
            }, {
                id: "5",
                color: "#3963bc",
                second: "#ecf5ff"
            }],
            theme: [{
                id: "dark-theme",
                label: "黑"
            }, {
                id: "light-theme",
                label: "白"
            }],
            loading: false,
            activeNames: [],
            ScheduledTasks:[{
                Scheduled_Name:'每日库存生成任务',
                TaskName:'inventoryLogicImpl',
                Method:'generateDailyInventory',
                Params:'0,1',
                Cron:'0 0 13 * * ?',
                timer:"23:00",
                time:'s',
                type:'1',
                istrue:false,
            },{
                Scheduled_Name:'每日销售价格生成任务',
                TaskName:'articleLogicImpl',
                Method:'generateDailyArticleSalesPrice',
                Params:'0,1',
                Cron:'0 0 23 * * ?',
                timer:"23:00",
                time:'s',
                type:'1',
                istrue:false,
            },{
                Scheduled_Name:'每日采购价格生成任务',
                TaskName:'articleLogicImpl',
                Method:'generateDailyArticlePurchasePrice',
                Params:'0,1',
                Cron:'0 0 23 * * ?',
                timer:"23:00",
                time:'s',
                type:'1',
                istrue:false,
            },{
                Scheduled_Name:'获取销售订单任务',
                TaskName:'salesOrderLogicImpl',
                Method:'generateSalesOrderByRedis',
                Params:'0',
                Cron:'*/5 * * * * ?',
                timer:"5",
                time:'s',
                type:'2',
                istrue:false,
            }],
            TaskLoading: true,
        }
    },
    mounted() {
        this.TimerTask();
    },
    methods: {
        getList() {
            const that = this;
            $.ajax({
                url: `${BASE_URL1}/getConfigAction?grant_type=tourist`,
                type: 'GET',
                success: function (data) {
                    if (data.length == 1) {
                        that.form = data[0];
                        console.log(that.form);
                    }
                },
                error: function () {
                    that.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        checkModify() {
            for (let key in this.form) {
                if (!key.includes('_Original')) {
                    if (this.form[key] != this.form[key + '_Original']) {
                        return false;
                    }
                }
            }
            return true;
        },
        generateForm(){
            let obj = {};
            for (let key in this.form) {
                if (!key.includes('_Original')&&key!='Operation_Datetime'&&key!='Operation_ID') {
                    obj[key] = this.form[key];
                }
            }
            return obj;
        },
        submitForm(formName) {
            const that = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (that.checkModify()) {
                        that.$message({
                            message: '当前设置并未改变！',
                            type: 'warning'
                        })
                        return
                    }
                    that.loading = true;
                    $.ajax({
                        url: `${BASE_URL2}/generalBusiness/generalSave`,
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({ Add: { base_config_info: [that.generateForm()] } })
                        },
                        success: function (res) {
                            if (res.Status === 'success') {
                                that.$message.success("提交成功")
                                that.getList();
                            } else {
                                that.$message.error("提交失败，请稍后再试")
                            }
                        },
                        error: function () {
                            that.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            that.loading = false
                            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                            }
                        }
                    })
                } else {
                    if (!that.activeNames.some(i => i == '1')) {
                        that.activeNames.push('1')
                    }
                    return false;
                }
            });
        },
        //定时器
        TimerTask(){
            const that = this;
            $.ajax({
                headers:requestHeaders(),
                url: `${BASE_URL2}/TimerTask/TimerTaskList`,
                type: 'GET',
                data:{},
                success: function (data) {
                    that.ScheduledTasks.forEach(item =>{
                        let is = true;
                        for (let key in data) {
                            if ((item.TaskName + item.Method) == key) {
                                item.istrue = true;
                                is = false;
                                break;
                            }
                        }
                        if (is) {
                            item.istrue = false;   
                        }
                    })
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                }
            })   
        },
        Scheduled(row){
            const that = this
            if (!row.istrue) {//开启定时器
                if (row.timer==null) {
                    this.$message({
                        message: '请选择执行时间',
                        type: 'warning'
                    })
                    return
                }
                if (row.type == '1') {//每日指定日期执行
                    let timer = row.timer.split(":")[0]
                    if (timer.charAt(0) == '0') {
                        timer = timer.charAt(1);
                    }
                    row.Cron = `0 0 ${timer} * * ?`
                }else if(row.type == '2'){//按时间区域
                    if (row.time == 's') {
                        row.Cron = `*/${row.timer} * * * * ?`
                    } else if (row.time == 'm') {
                        row.Cron = `0 */${row.timer} * * * ?`
                    } else if (row.time == 'h') {
                        row.Cron = `0 0 */${row.timer} * * ?`
                    }
                }
                $.ajax({
                    headers:requestHeaders(),
                    url: `${BASE_URL2}/TimerTask/exectueTimerTask`,
                    type: 'GET',
                    data: {
                        TaskName:row.TaskName,
                        Method:row.Method,
                        Params:row.Params,
                        Cron:row.Cron
                    },
                    complete: function (XMLHttpRequest) {
                        if (XMLHttpRequest.status == 200) {
                            that.$message.success("已开启");
                            that.TimerTask();
                        }else{
                            that.$message.error("系统繁忙");
                            requestPretreatment(XMLHttpRequest);
                        }
                    }
                })   
            } else {//关闭定时器
                $.ajax({
                    headers:requestHeaders(),
                    url: `${BASE_URL2}/TimerTask/cancelTimerTask`,
                    type: 'GET',
                    data: {
                        TaskName:row.TaskName,
                        Method:row.Method,
                        Params:row.Params,
                    },
                    complete: function (XMLHttpRequest) {
                        if (XMLHttpRequest.status == 200) {
                            that.$message.success("已关闭");
                            that.TimerTask();
                        }else{
                            that.$message.error("系统繁忙");
                            requestPretreatment(XMLHttpRequest);
                        }
                    }
                }) 
            }
        },
        // getRides(key,callback){//查redis
        //     $.ajax({
        //         url: `${BASE_URL1}/GetRedis?Key=${key}`,
        //         type: 'GET',
        //         success: function (Result) {
        //             callback?callback(Result):null
        //         },
        //         dataType: "json"
        //     });
        // },
        // setRides(Key,Main,callback){//存redis
        //     $.ajax({
        //         url: `${BASE_URL1}/RedisAdd`,
        //         type: 'POST',
        //         data:{Key,Main},
        //         success: function (Result) {
        //             callback?callback(Result):null;
        //         },
        //         dataType: "json"
        //     });
        // }
    }
})